<template>
  <div id="code-editor" ref="codeEditorRef" style="width: 100%" />
</template>

<script setup lang="ts">
import * as monaco from "monaco-editor";
import { defineProps, onMounted, ref, toRaw, withDefaults } from "vue";

const codeEditorRef = ref();
const codeEditor = ref();

interface Props {
  text: string;
  language: string;
  handleChange: (v: string) => void;
}

const props = withDefaults(defineProps<Props>(), {
  text: "",
  language: "java",
  handleChange: (v: string) => {
    console.log("默认方法:", v);
  },
});

onMounted(() => {
  if (!codeEditorRef.value) {
    return;
  }
  // Hover on each property to see its docs!
  codeEditor.value = monaco.editor.create(codeEditorRef.value, {
    value: props.text,
    language: props.language,
    automaticLayout: true,
    colorDecorators: true,
    minimap: {
      enabled: false,
    },
    readOnly: false,
    theme: "vs-dark",
    lineNumbers: "on",
    // roundedSelection: false,
    // scrollBeyondLastLine: false,
  });

  // 编辑 监听内容变化
  codeEditor.value.onDidChangeModelContent(() => {
    const temp = toRaw(codeEditor.value).getValue();
    props.handleChange(temp);
    // console.log("目前内容为：", toRaw(codeEditor.value).getValue());
  });
});
</script>

<style scoped></style>

<!--<template>-->
<!--  <div>-->
<!--    &lt;!&ndash;    <MarkdownEditor :value="val" :handle-change="handleChange"></MarkdownEditor>&ndash;&gt;-->
<!--    &lt;!&ndash;    <CodeEditor&ndash;&gt;-->
<!--    &lt;!&ndash;      :text="val"&ndash;&gt;-->
<!--    &lt;!&ndash;      :handle-change="handleChange"&ndash;&gt;-->
<!--    &lt;!&ndash;      style="width: 900px; height: 500px"&ndash;&gt;-->
<!--    &lt;!&ndash;    />&ndash;&gt;-->
<!--  </div>-->
<!--</template>-->
<!--<script setup lang="ts">-->
<!--import { ref } from "vue";-->
<!--import CodeEditor from "@/components/CodeEditor.vue";-->

<!--let val = ref("www");-->
<!--let handleChange = (v: string) => {-->
<!--  val.value = v;-->
<!--};-->
<!--</script>-->
<!--<style></style>-->
